<?php

namespace Home\Model;
use Think\Model;

class ReportModel extends Model {
	protected $tableName = 'report';
	private $services_table = 'dmp_services';
	private $image_api_table = 'dmp_service_apis_image';

	//新增报告
	public function insertReport($data){
		if(empty($data)) return false;

		$data['create_time'] = $data['update_time'] = time();
		return $this->data($data)->add();
	}

	//更新报告
	public function updateReport($data){
		if(empty($data)) return false;
		if(!isset($data['id'])) return false;

		$data['update_time'] = time();
		$where['id'] = $data['id'];
		$where['uid'] = $data['uid'];
		return $this->where($where)->save($data);
	}

	// 用户发布报告列表
	public function getListWithUser($uid=null,$page=1,$pagesize=10){
		
		$where['uid'] = $uid;
		$where['status'] = 1;//可用
		$count = $this->where($where)->count();

		$offset = ($page-1)*$pagesize;

		$list = $this->where($where)->limit($offset.",".$pagesize)->select();
		$pagenum = ceil($count/$pagesize);
		return array('page'=>$page,'count'=>$count,'data'=>$list,'totalpage'=>$pagenum);

	}

	//获取所有报告列表
	public function getList($page=1,$pagesize=10,$getcontent = false){
		$where['status'] = 1;//可用
		$count = $this->where($where)->count();

		$offset = ($page-1)*$pagesize;
		if($getcontent){
			$field = '*';
		}else{
			$field = 'id,title,attachment,cover_img,desc,price,related_service_ids,use_nums,attachment_name,status';
		}
		$list = $this->field($field)->where($where)->limit($offset.",".$pagesize)->select();

		return array('page'=>$page,'count'=>$count,'data'=>$list);
	}
	
	//获取报告详情
	public function info($id,$getcontent=true){
		if(!is_numeric($id) || !$id) return null;

		$where['id'] = $id;

		if($getcontent){
			$field = '*';
		}else{
			$field = 'id,title,attachment,cover_img,desc,price,related_service_ids,use_nums,attachment_name,status';
		}
		return $this->field($field)->where($where)->find();

	}

	//获取报告详情及报告服务商信息，下载用户是否购买
	public function fullInfo($id= null,$uid = 0){
		if(!is_numeric($id) || !$id) return null;

		// $id = 1;
		$sql = "SELECT report.id,report.title,report.attachment,report.cover_img,report.desc,report.price,";
		$sql .=" report.related_service_ids,report.use_nums,report.attachment,report.status,report.create_time,report.content,report.update_time,";
		$sql .=" CASE user.auth_type WHEN 1 then user.user_name WHEN 2 then enter.enterprise_name END AS uname,";
 		$sql .="`order`.pay_status";
		
		$sql .=" FROM dmp_report report LEFT JOIN dmp_users user ON user.id = report.uid ";
		$sql .=" LEFT JOIN dmp_enterprises enter ON user.enterprise_id != '' AND enter.id = user.enterprise_id ";
		if(empty($uid)){
			$sql .="LEFT JOIN dmp_report_orders `order` ON `order`.report_id=".$id." and `order`.pay_status=1";
		}else{
			$sql .="LEFT JOIN dmp_report_orders `order` ON `order`.uid = ".$uid." and `order`.report_id=".$id." and `order`.pay_status=1";
		}
		

		$sql .=" where report.id=".$id;
		$info = $this->query($sql);

		if(!empty($info)){
			return $info[0];
		}else{
			return null;
		}
		
	}

	//获取报告相关服务数据
	public function getRelatedServices($related_id_array = array()){
		if(empty($related_id_array)) return null;

		$related_id_string = implode(",",$related_id_array);
		$sql = "select ser.*,u.auth_type,ifnull(least(min(api.min_price),if(api.api_is_charge=0,0,99999999)),0) price,group_concat(DISTINCT tag.name) tag_string from ".$this->services_table."  ser left join ".$this->image_api_table." api on api.service_id=ser.service_id and api.api_status>0 left join dmp_service_tag ser_tag on ser_tag.service_id=ser.service_id left join dmp_tag tag on tag.id=ser_tag.tag_id left join dmp_users u on u.id=ser.uid where ser.service_id in(".$related_id_string .")  and ser.service_status=1 group by ser.service_id";

		// echo $sql;die;
		$list = $this->query($sql);
		return $list;
	}
}

?>